package com.dot.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.dot.dao.DwarfPageDao;
import com.dot.vo.DwarfPageVo;

public class DwarfPageDaoImpl implements DwarfPageDao{

	JdbcTemplate jdbcTemplate;
	
	public void save(DwarfPageVo dwarfPage) throws Exception {
		String sql = "insert into dwarf_page (book_code, page_url, page_num )values(?,?,?);";
		Object[] args = new Object[]{dwarfPage.getBookCode(),dwarfPage.getPageUrl(), dwarfPage.getPageNum()};
		jdbcTemplate.update(sql, args);
	}

	public List<DwarfPageVo> getByBookCode(String bCode) throws Exception {
		List<DwarfPageVo> pages = this.jdbcTemplate.query(
				"select id, book_code, page_url, page_num from dwarf_page where book_code = ?;",
				new Object[]{bCode},
				new RowMapper<DwarfPageVo>(){
					public DwarfPageVo mapRow(ResultSet rs, int rowNum) throws SQLException {
						DwarfPageVo page = new DwarfPageVo();
						page.setId(rs.getLong("id"));
						page.setBookCode(rs.getString("book_code"));
						page.setPageUrl(rs.getString("page_url"));
						page.setPageNum(rs.getInt("page_num"));
						return page;
					}
			});
		return pages;
	}

}
